package com.xw.mailservice.mail.listener;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.event.TransportEvent;
import javax.mail.event.TransportListener;

import com.xw.mailservice.third.UccApi;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 描述:邮件发送的监听器
 * @author 杨建全
 * @date 2017年5月26日 下午4:24:42
 */
@Component
public class SendMailTransportListener implements TransportListener{

	private static final Logger logger = LoggerFactory.getLogger(SendMailTransportListener.class);
	
	@Autowired
	private UccApi uccApi;
	
	@Override
	public void messageDelivered(TransportEvent e) {
		Message message = e.getMessage();
		try {
			logger.info("邮件【"+message.getSubject()+"】发送成功！");
		} catch (MessagingException e1) {
			logger.error("",e1);
		}
		uccApi.sendResult(e);
	}

	@Override
	public void messageNotDelivered(TransportEvent e) {
		Message message = e.getMessage();
		try {
			logger.info("邮件【"+message.getSubject()+"】发送失败！");
		} catch (MessagingException e1) {
			logger.error("",e1);
		}
		uccApi.sendResult(e);
	}

	@Override
	public void messagePartiallyDelivered(TransportEvent e) {
		Message message = e.getMessage();
		try {
			logger.info("邮件【"+message.getSubject()+"】部分发送成功！");
		} catch (MessagingException e1) {
			logger.error("",e1);
		}
		uccApi.sendResult(e);
	}
}
